Skip to contentSkip to conversation

Change in openvpn[master]: Avoid recursive call of man_read

Openvpn-devel selvanair (Code Review) 1 participant 5 messages Apr 27 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): gerrit.1777311203000.I9433baee1f5a66d43d4e43e03128ae6a790a7937@...2715...

#1 lore raw

Attention is currently required from: plaisthos.

Hello plaisthos,

I'd like you to do a code review.
Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1639?usp=email

to review the following change.

Change subject: Avoid recursive call of man_read
......................................................................

Avoid recursive call of man_read

Recursion to man_read() happens on Windows when man_process_command tries to log the received command because management_io first checks FD_READ and calls man_read() again if there is data is pending in the receive buffer. This is buggy as man_read() is not designed to be re-entrant.

Fix this by disallowing recursion -- just return 0 so that managament_io will complete without recursion. Although this will also clear FD_READ from the event mask via

    `net_event_win32_clear_selected_events(&man->connection.ne32, FD_READ)`

(manage.c:3312), read will get serviced next time when the main event loop calls management_io thorugh process_io. Note that management_io always calls reset_net_event_win32() (manage.c: 3298) which will pickup if any data to read is pending (win32.c: 357) as FD_READ is level triggered.

Giithub: Fixes OpenVPN/openvpn#1023

Change-Id I9433baee1f5a66d43d4e43e03128ae6a790a7937

---
M src/openvpn/manage.c

1 file changed, 8 insertions(+), 0 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/1639/1

src/openvpn/manage.c +8 -0
index 2a3023c..4d46e61 100644
@@ -2425,6 +2425,13 @@
      */
     unsigned char buf[256];
     ssize_t len = 0;
+    static int recursive_level = 0;
+
+    if (recursive_level > 0)
+    {
+        return 0; /* not ready to read now */
+    }
+    recursive_level++;
 
 #ifdef TARGET_ANDROID
     int fd;
@@ -2508,6 +2515,7 @@
             man_reset_client_socket(man, false);
         }
     }
+    recursive_level--;
     return len;
 }
 

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1639?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9433baee1f5a66d43d4e43e03128ae6a790a7937
Gerrit-Change-Number: 1639
Gerrit-PatchSet: 1
Gerrit-Owner: selvanair <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>

#2 lore raw
In reply to a message not in the archive

Attention is currently required from: selvanair.

plaisthos has posted comments on this change by selvanair. ( http://gerrit.openvpn.net/c/openvpn/+/1639?usp=email )

Change subject: Avoid recursive call of man_read
......................................................................

Patch Set 1: Code-Review+1

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1639?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9433baee1f5a66d43d4e43e03128ae6a790a7937
Gerrit-Change-Number: 1639
Gerrit-PatchSet: 1
Gerrit-Owner: selvanair <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: selvanair <[email protected]...>
Gerrit-Comment-Date: Mon, 27 Apr 2026 19:27:01 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes

#3 lore raw
In reply to a message not in the archive

selvanair has posted comments on this change by selvanair. ( http://gerrit.openvpn.net/c/openvpn/+/1639?usp=email )

Change subject: Avoid recursive call of man_read
......................................................................

Patch Set 1:

(1 comment)

Patchset:

PS1:
Although FD_READ is level triggered,MSDN has this:
".. an application need not read all available data in response to an FD_READ network event—a single recv in response to each FD_READ network event is appropriate."

This is problematic, as it requires at least one recv() to get the event to re-trigger. So, man_read() retruning without recv() as proposed here may not be fool-proof? Unfortunately recv with zero bytes is a no-op, so that's not an option either.

Back to square one?

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1639?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9433baee1f5a66d43d4e43e03128ae6a790a7937
Gerrit-Change-Number: 1639
Gerrit-PatchSet: 1
Gerrit-Owner: selvanair <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Comment-Date: Mon, 27 Apr 2026 20:14:31 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No

#4 lore raw
In reply to a message not in the archive

Attention is currently required from: plaisthos.

Hello plaisthos,

I'd like you to reexamine a change. Please visit

    http://gerrit.openvpn.net/c/openvpn/+/1639?usp=email

to look at the new patch set (#2).

The following approvals got outdated and were removed:
Code-Review+1 by plaisthos

Change subject: Avoid recursive call of man_read
......................................................................

Avoid recursive call of man_read

Recursion to man_read() happens on Windows when man_process_command tries to log the received command because management_io first checks FD_READ and calls man_read() again if there is data is pending in the receive buffer. This is buggy as man_read() is not designed to be re-entrant.

Fix this by disallowing recursion -- just return 0 so that managament_io will complete without recursion. Check this condition and do not clear FD_READ from the event mask in management_io when man_read returns without calling recv(). Read will get serviced next time when the main event loop calls management_io thorugh process_io. Note that management_io always calls reset_net_event_win32() (manage.c: 3298) which will pickup if any new data to read is pending (win32.c: 357) as well as retain FD_READ if already set.

Giithub: Fixes OpenVPN/openvpn#1023

Change-Id I9433baee1f5a66d43d4e43e03128ae6a790a7937

---
M src/openvpn/manage.c
M src/openvpn/manage.h

2 files changed, 16 insertions(+), 2 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/39/1639/2

src/openvpn/manage.c +13 -2
index 2a3023c..31c4bb5 100644
@@ -2426,6 +2426,12 @@
     unsigned char buf[256];
     ssize_t len = 0;
 
+    if (man->connection.man_read_state != MAN_READ_IDLE)
+    {
+        return 0; /* not ready to read now */
+    }
+    man->connection.man_read_state = MAN_READ_ACTIVE;
+
 #ifdef TARGET_ANDROID
     int fd;
     len = man_recv_with_fd(man->connection.sd_cli, buf, sizeof(buf), MSG_NOSIGNAL, &fd);
@@ -2508,6 +2514,7 @@
             man_reset_client_socket(man, false);
         }
     }
+    man->connection.man_read_state = MAN_READ_IDLE;
     return len;
 }
 
@@ -3309,10 +3316,14 @@
             {
                 if (net_events & FD_READ)
                 {
-                    while (man_read(man) > 0)
+                    if (man->connection.man_read_state == MAN_READ_IDLE)
                     {
+                        while (man_read(man) > 0)
+                        {
+                        }
+                        net_event_win32_clear_selected_events(&man->connection.ne32, FD_READ);
                     }
-                    net_event_win32_clear_selected_events(&man->connection.ne32, FD_READ);
+                    /* if ACTIVE: reentrancy, skip read and skip clearing FD_READ */
                 }
 
                 if (net_events & FD_WRITE)
src/openvpn/manage.h +3 -0
index 27d3b60..0632500 100644
@@ -328,6 +328,9 @@
     int lastfdreceived;
 #endif
     int client_version;
+#define MAN_READ_IDLE   0
+#define MAN_READ_ACTIVE 1
+    int man_read_state;
 };
 
 struct management

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1639?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9433baee1f5a66d43d4e43e03128ae6a790a7937
Gerrit-Change-Number: 1639
Gerrit-PatchSet: 2
Gerrit-Owner: selvanair <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>

#5 lore raw
In reply to a message not in the archive

Attention is currently required from: plaisthos.

selvanair has posted comments on this change by selvanair. ( http://gerrit.openvpn.net/c/openvpn/+/1639?usp=email )

Change subject: Avoid recursive call of man_read
......................................................................

Patch Set 2:

(1 comment)

Patchset:

PS1:

Quoted 1 line

Although FD_READ is level triggered,MSDN has this: […]

Pushed a new version that calls man_read and clears FD_READ only if recursion would not happen. This should address the concern above..

--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/1639?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings?usp=email

Gerrit-MessageType: comment
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I9433baee1f5a66d43d4e43e03128ae6a790a7937
Gerrit-Change-Number: 1639
Gerrit-PatchSet: 2
Gerrit-Owner: selvanair <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Comment-Date: Mon, 27 Apr 2026 20:56:36 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: selvanair <[email protected]...>